package sit.engreal.leetcode.problems200;

import org.junit.jupiter.api.Assertions;

/**
 * <a herf="https://leetcode.cn/problems/rotate-array/">189. 轮转数组</a>
 */
public class LeetCode189 {
    public static class Solution {
        public void rotate(int[] nums, int k) {
            k %= nums.length;
            reverse(nums, 0, nums.length);
            reverse(nums, 0, k);
            reverse(nums, k, nums.length);
        }

        private void reverse(int[] array, int begin, int end) {
            for (int i = begin, j = end - 1; i < j; i++, j--) {
                int tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        final Solution solution = new Solution();

        {
            int[] input = {-1};
            int[] expect = {-1};
            solution.rotate(input, 2);
            Assertions.assertArrayEquals(expect, input);
        }

        {
            int[] input = {1, 2, 3, 4, 5, 6, 7};
            int[] expect = {5, 6, 7, 1, 2, 3, 4};
            solution.rotate(input, 3);
            Assertions.assertArrayEquals(expect, input);
        }
    }
}
